﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class StateMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsCity clsCity = new clsCity();
                FillState();
                FillddlCountry();
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
    protected void FillState()
    {
        try
        {
            List<IN_TB_STATE> objIN_TB_STATEist = new List<IN_TB_STATE>();
            clsCity objIN_TB_STATEDAL = new clsCity();
            objIN_TB_STATEist = objIN_TB_STATEDAL.GetAllStates();
            if (objIN_TB_STATEist != null)
            {
                grdState.DataSource = objIN_TB_STATEist;
                grdState.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void FillddlCountry()
    {
        try
        {
            clsCity objclsCity = new clsCity();
            List<IN_TB_COUNTRY> objIN_TB_COUNTRY = new List<IN_TB_COUNTRY>();
            objIN_TB_COUNTRY = objclsCity.GetAllCountry ();
            ddlCountry.Items.Clear();
            ddlCountry.AppendDataBoundItems = true;
            ddlCountry.DataTextField = "COUNTRY";
            ddlCountry.DataValueField = "ID";
            ddlCountry.Items.Add("Select");
            ddlCountry.Items[0].Value = "0";
            ddlCountry.DataSource = objIN_TB_COUNTRY;
            ddlCountry.DataBind();

        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void ClearAllControls()
    {
        try
        {
            txtStatename.Text = string.Empty;
            ddlCountry.SelectedIndex = 0;
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void btnState_Click(object sender, EventArgs e)
    {


        try
        {
            if (Session["StateId"] == null)
            {
                clsCity objclsCity = new clsCity();
                IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
                objIN_TB_STATE.STATE = txtStatename.Text;
                objIN_TB_STATE.COUNTRY_ID = Convert.ToInt32(ddlCountry.SelectedValue);
                int cityid = objclsCity.SaveStateMst (objIN_TB_STATE);
               ClearAllControls();
                FillState();
            }
            else
            {
                clsCity objclsCity = new clsCity();
                IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
                objIN_TB_STATE.ID = Convert.ToInt32(Session["StateId"].ToString());
                objIN_TB_STATE.STATE = txtStatename.Text;
                objIN_TB_STATE.COUNTRY_ID = Convert.ToInt32(ddlCountry.SelectedValue);
                int Stateid = objclsCity.SaveStateMst(objIN_TB_STATE);
                FillState();
              ClearAllControls();
                Session["StateId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }

    }
    protected void grdState_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Getting State Names
                int CountryId = Convert.ToInt32(e.Row.Cells[1].Text);
                clsCity objclsCity = new clsCity();
                IN_TB_COUNTRY objIN_TB_COUNTRY = new IN_TB_COUNTRY();
                objIN_TB_COUNTRY = objclsCity.GetCountryById(CountryId);
                e.Row.Cells[1].Text = objIN_TB_COUNTRY.COUNTRY;

            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdState_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdState.PageIndex = e.NewPageIndex;
        FillState();

    }
    protected void grdState_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int StateId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_STATEDAL = new clsCity();
                IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
                objIN_TB_STATE = objIN_TB_STATEDAL.GetStateById(StateId);
                if (objIN_TB_STATE != null)
                {
                    ddlCountry.SelectedValue = objIN_TB_STATE.COUNTRY_ID.ToString();
                    txtStatename.Text = objIN_TB_STATE.STATE;
                    Session["StateId"] = StateId;
                }
            }

            if (e.CommandName == "Del")
            {
                int StateId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_STATEDAL = new clsCity();
                IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
                objIN_TB_STATEDAL.DeleteState(StateId);
                FillState();
            }
        }
        catch (Exception)
        {

            throw;
        }



    }
}
